<template>
  <router-view></router-view>
  <el-table :data="paginatedData" border style="width: 100%">
    <el-table-column prop="account" label="账号" width="180" align="center" />
    <el-table-column prop="num" label="发布照片数量" width="180" align="center" />
    <el-table-column label="发布照片" align="center">
      <template #default="{ row }">
        <div v-if="row.photos.length">
          <div style="display: flex; flex-wrap: nowrap; overflow-x: auto;">
            <div v-for="(photo, index) in row.photos" :key="index" style="margin-right: 5px;">
              <router-link :to="{ path: '/photo/detail', query: { id: photo } }">
                <img :src="photo" alt="photo" style="width: 100px; height: 188px;" />
              </router-link>
            </div>
          </div>
        </div>
        <span v-else>无照片</span>
      </template>
    </el-table-column>
  </el-table>
  <div class="page">
    <el-pagination
      size="small"
      background
      layout="prev, pager, next"
      :total="totalItems"
      :page-size="itemsPerPage"
      :current-page="currentPage"
      @current-change="handlePageChange"
      class="mt-4"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref, computed,onMounted } from 'vue';
import { photo_list } from '../../api/request';
import api from '../../api/index';

interface User {
  account: string;
  num: number;
  photos: string[];
}

const tableData = ref<User[]>([]);
const currentPage = ref(1);
const itemsPerPage = 2; // 每页显示的行数

const totalItems = computed(() => tableData.value.length); // 总行数

const paginatedData = computed(() => {
  const start = (currentPage.value - 1) * itemsPerPage;
  return tableData.value.slice(start, start + itemsPerPage); // 获取当前页的数据
});

const allphotos = async () => {
  const content = await photo_list();
  const { code, result } = content;

  if (code === 200) {
    tableData.value = result.map((item) => {
      const photos = item.photo
        .split('\n')
        .map((photoPath: string) => photoPath.trim())
        .filter(Boolean)
        .map(photoPath => api.url + photoPath);
      return {
        account: item.account,
        num: photos.length,
        photos: photos
      };
    });
  }
};

const handlePageChange = (page: number) => {
  currentPage.value = page; // 更新当前页码
};

onMounted(() => {
  allphotos();
});
</script>

<style scoped>
.page {
  margin-top: 20px;
  width: 100%; 
  height: auto; 
  margin: 0 auto;
  display: flex; 
  justify-content: center; 
  align-items: center; 
  padding: 20px 0; 
}
</style>